package com.ridaizu.bean;

import java.io.Serializable;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.primefaces.context.RequestContext;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.LazyDataModel;

import com.google.common.base.Strings;
import com.ridaizu.cache.CacheDataBean;
import com.ridaizu.dao.db.DBManager;
import com.ridaizu.lazy.LazyGoodsTypeDataModel;
import com.ridaizu.model.GoodsType;
import com.ridaizu.model.form.GoodsTypeForm;
import com.ridaizu.model.search.GoodsTypeSearch;
import com.ridaizu.service.impl.GoodsTypeServiceImpl;
import com.ridaizu.util.DateUtil;

@javax.faces.bean.ManagedBean(name = "goodsTypeBean")
@SessionScoped
public class GoodsTypeBean implements Serializable {

	private static final long serialVersionUID = 1L;

	private LazyDataModel<GoodsType> lazyModel;

    @ManagedProperty("#{cacheDataBean}")
    private CacheDataBean cacheDataBean;

    @ManagedProperty("#{goodsTypeServiceImpl}")
    private GoodsTypeServiceImpl goodsTypeService;

    @ManagedProperty("#{goodsTypeSearch}")
    private GoodsTypeSearch goodsTypeSearch;

    @ManagedProperty("#{goodsTypeForm}")
    private GoodsTypeForm goodsTypeForm;

	public void searchGoodsTypeSync() {
		lazyModel = new LazyGoodsTypeDataModel(goodsTypeSearch);
	}

	public void viewRegisterGoodsType() throws SQLException {

		setInitValues(null);

        Map<String,Object> options = new HashMap<String, Object>();
        options.put("modal", true);
        options.put("contentWidth", 550);
        options.put("contentHeight", 200);

        RequestContext.getCurrentInstance().openDialog("goods-type", options, null);
    }

	public void viewUpdateGoodsType(GoodsType goodsType) throws SQLException {

		setInitValues(goodsType);

        Map<String,Object> options = new HashMap<String, Object>();
        options.put("modal", true);
        options.put("contentWidth", 550);
        options.put("contentHeight", 220);

        RequestContext.getCurrentInstance().openDialog("goods-type", options, null);
    }

	private void setInitValues(GoodsType goodsType) throws SQLException {
		goodsTypeForm.clearAll();
		setValues(goodsType);
	}

	private void setValues(GoodsType goodsType) {
		if (goodsType == null) {
			setRegisterValues();
		} else {
			setUpdateValues(goodsType);
		}
	}

	private void setRegisterValues() {
	}

	private void setUpdateValues(GoodsType goodsType) {
		goodsTypeForm.setId(String.valueOf(goodsType.getId()));
		goodsTypeForm.setName(goodsType.getName());
		goodsTypeForm.setDelFlg(goodsType.isDelFlg());
		goodsTypeForm.setRgtOpt(goodsType.getRgtOpt());
		goodsTypeForm.setRgtDate(new Date(goodsType.getRgtDate().getTime()));
		goodsTypeForm.setUpdOpt(goodsType.getUpdOpt());
		goodsTypeForm.setUpdDate(new Date(goodsType.getUpdDate().getTime()));
	}

	public void registerGoodsType() {
		GoodsType goodsType = getGoodsType();

		boolean result = false;

		try {
			DBManager.beginTransaction();
			result = goodsTypeService.insertModel(goodsType);
			DBManager.commitTransaction();

			cacheDataBean.setGoodsTypeSelectItemList(null);
		} catch (SQLException e) {
			e.printStackTrace();
			result = false;
			DBManager.rollbackTransaction();
		}

		RequestContext.getCurrentInstance().closeDialog(result);
    }

	public void updateGoodsType() {

		GoodsType goodsType = getGoodsType();

		boolean result = false;

		try {
			DBManager.beginTransaction();
			result = goodsTypeService.updateModel(goodsType);
			DBManager.commitTransaction();

			cacheDataBean.setGoodsTypeSelectItemList(null);
		} catch (SQLException e) {
			e.printStackTrace();
			result = false;
			DBManager.rollbackTransaction();
		}

		RequestContext.getCurrentInstance().closeDialog(result);
	}

	public void deleteGoodsType() {

		GoodsType selectedGoodsType = goodsTypeForm.getSelectedGoodsType() ;

		if (selectedGoodsType == null || selectedGoodsType.getId() < 1) {
	        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Message", "请选择要删除的商品目录信息。");
	        FacesContext.getCurrentInstance().addMessage(null, message);

	        return ;
		}

		boolean result = false;
		try {
			DBManager.beginTransaction();
			result = goodsTypeService.deleteModel(selectedGoodsType.getId());
			DBManager.commitTransaction();
		} catch (SQLException e) {
			e.printStackTrace();
			result = false;
			DBManager.rollbackTransaction();
		}

		String resultStr = null;
		if (result) {
			cacheDataBean.setGoodsTypeSelectItemList(null);
			resultStr = String.format("商品目录[%s]删除成功。", selectedGoodsType.getId());
		} else {
			resultStr = String.format("商品目录[%s]删除失败。", selectedGoodsType.getId());
		}

        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "GoodsType delete result", resultStr);
        FacesContext.getCurrentInstance().addMessage(null, message);
	}

	public void registerGoodsTypeResult(SelectEvent event) {
		boolean result = (Boolean) event.getObject();

		handleResult(result, "登录");
	}

	public void updateGoodsTypeResult(SelectEvent event) {

		boolean result = (Boolean) event.getObject();

		handleResult(result, "更新");
	}

	private void handleResult(boolean result, String optStr) {

		String resultStr = null;
		if (result) {
			resultStr = String.format("商品目录信息%s成功。", optStr);
		} else {
			resultStr = String.format("商品目录信息%s失败。", optStr);;
		}

        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Message", resultStr);

        FacesContext.getCurrentInstance().addMessage(null, message);
	}

	private GoodsType getGoodsType() {

		GoodsType goodsType = new GoodsType();

		if (!Strings.isNullOrEmpty(goodsTypeForm.getId())) {
			goodsType.setId(Integer.valueOf(goodsTypeForm.getId()));
		}

		goodsType.setName(goodsTypeForm.getName());
		goodsType.setDelFlg(goodsTypeForm.isDelFlg());

		Timestamp currentDate = DateUtil.getCurrentDateTime();

		goodsType.setRgtOpt("Admin");
		goodsType.setRgtDate(currentDate);
		goodsType.setUpdOpt("Admin");
		goodsType.setUpdDate(currentDate);

		return goodsType;
	}

	public LazyDataModel<GoodsType> getLazyModel() {
		return lazyModel;
	}

	public void setLazyModel(LazyDataModel<GoodsType> lazyModel) {
		this.lazyModel = lazyModel;
	}

	public GoodsTypeServiceImpl getGoodsTypeService() {
		return goodsTypeService;
	}

	public void setGoodsTypeService(GoodsTypeServiceImpl goodsTypeService) {
		this.goodsTypeService = goodsTypeService;
	}

	public GoodsTypeSearch getGoodsTypeSearch() {
		return goodsTypeSearch;
	}

	public void setGoodsTypeSearch(GoodsTypeSearch goodsTypeSearch) {
		this.goodsTypeSearch = goodsTypeSearch;
	}

	public GoodsTypeForm getGoodsTypeForm() {
		return goodsTypeForm;
	}

	public void setGoodsTypeForm(GoodsTypeForm goodsTypeForm) {
		this.goodsTypeForm = goodsTypeForm;
	}

	public CacheDataBean getCacheDataBean() {
		return cacheDataBean;
	}

	public void setCacheDataBean(CacheDataBean cacheDataBean) {
		this.cacheDataBean = cacheDataBean;
	}

}